home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / lib / unix / fstat.c < prev    next >
C/C++ Source or Header  |  1997-09-09  |  3KB  |  103 lines

  1.  
  2. /*
  3.  *  FSTAT call (only works properly under 2.0)
  4.  *
  5.  *    (c)Copyright 1992-1997 Obvious Implementations Corp.  Redistribution and
  6.  *    use is allowed under the terms of the DICE-LICENSE FILE,
  7.  *    DICE-LICENSE.TXT.
  8.  */
  9.  
  10. #include <exec/types.h>
  11. #include <dos/dos.h>
  12. #include <dos/dosextens.h>
  13. #include <clib/dos_protos.h>
  14. #include <sys/stat.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <fcntl.h>
  18. #include <string.h>
  19. #include <errno.h>
  20. #include <lib/misc.h>
  21.  
  22. extern struct DosLibrary *DOSBase;
  23.  
  24. typedef struct FileInfoBlock    FileInfoBlock;
  25.  
  26. fstat(fd, xstat)
  27. int fd;
  28. struct stat *xstat;
  29. {
  30.     __aligned FileInfoBlock fib;
  31.     int r = -1;
  32.     _IOFDS *d;
  33.  
  34.     clrmem(xstat, sizeof(*xstat));
  35.     if (d = __getfh(fd)) {    /*  2.0 */
  36.     if (DOSBase->dl_lib.lib_Version >= 36) {
  37.         if (ExamineFH(d->fd_Fh, (FileInfoBlock *) &fib)) {
  38.         xstat->st_size = fib.fib_Size;
  39.         xstat->st_ino =  fib.fib_DiskKey;
  40.         xstat->st_dev = (long)((struct FileHandle *)BADDR(d->fd_Fh))->fh_Type;
  41.         xstat->st_mode = (fib.fib_DirEntryType > 0) ? S_IFDIR : S_IFREG;
  42.         xstat->st_ctime = xstat->st_mtime = fib.fib_Date.ds_Days * (1440 * 60) +
  43.                         fib.fib_Date.ds_Minute * 60 +
  44.                         fib.fib_Date.ds_Tick / 50 + _TimeCompensation;
  45.         if ((fib.fib_Protection & 8) == 0)
  46.             xstat->st_mode |= S_IREAD;
  47.         if ((fib.fib_Protection & 4) == 0)
  48.             xstat->st_mode |= S_IWRITE;
  49.         if ((fib.fib_Protection & 2) == 0)
  50.             xstat->st_mode |= S_IEXEC;
  51.         if (fib.fib_Protection & 0x40)
  52.             xstat->st_mode |= S_IEXEC;
  53.  
  54.         r = 0;
  55.         }
  56.         else {
  57.         // if packet not supported it means we are talking to
  58.             // a handler, so we have to fake it from flags
  59.         if(IoErr() == ERROR_ACTION_NOT_KNOWN ) {
  60.             if(((FILE *)fd)->sd_Flags & __SIF_READ)xstat->st_mode |= S_IREAD;
  61.             if(((FILE *)fd)->sd_Flags & __SIF_WRITE)xstat->st_mode |= S_IWRITE;
  62.             r = 0;
  63.         }
  64.         }
  65.     } else {        /*  1.3 */
  66.         r = stat(d->fd_FileName, xstat);
  67.     }
  68.     /*
  69.      *  extended size will not show up in examine if we have written
  70.      *  the active handle.
  71.      */
  72.     {
  73.         long pos = Seek(d->fd_Fh, 0L, 0);
  74.         long siz;
  75.         Seek(d->fd_Fh, 0L, 1);
  76.         siz = Seek(d->fd_Fh, pos, -1);
  77.         if (xstat->st_size < siz)
  78.         xstat->st_size = siz;
  79.     }
  80.     }
  81.     if (r < 0)errno = ENOENT;
  82.     return(r);
  83. }
  84.  
  85. #ifdef TEST
  86.  
  87. main(ac, av)
  88. char *av[];
  89. {
  90.     struct stat xstat;
  91.     short i;
  92.     int fd;
  93.  
  94.     for (i = 1; i < ac; ++i) {
  95.     int fd = open(av[i], O_RDONLY);
  96.     int r = fstat(fd, &xstat);
  97.     printf("fd = %d r = %d fs=%d ti=%08lx\n", fd, r, xstat.st_size, xstat.st_ctime);
  98.     }
  99.     return(0);
  100. }
  101.  
  102. #endif
  103.